﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eComet
{
    public class PlugIn : ECometPlugIn,IECometPlugIn
    {



        #region Module Members

        public override ECometConfig Configuration()
        {
            return new ECometConfig("Tagger",".mp3,.wma,.m4a,.mp4",true,true,true,true,true);
        }

        public override void Actions(int selectedId)
        {
            //if (commands.ContainsKey(selectedId))
            //{
            //    List<String> lstCommand = commands[selectedId];
            //    String command = lstCommand[0];
            //    String args = lstCommand[1];
            //    MessageBox.Show(command + " " + args);
            //}
            RunCommand(selectedId);
            //MessageBox.Show("Tagger recibio " + selectedId.ToString());
            //throw new NotImplementedException();
        }

        public override int Menu(List<String> paths, List<String> extensions, uint numFiles, int typeDirOrFile)
        {
            String nomApp = "eCometTagger";
            String directory = String.Join(";", paths.ToArray());
            String fullPaths = directory.Replace(' ','¬');

            if(numFiles==1)
            {
                if (typeDirOrFile==1)
                {
                    string[] dirs = directory.Split('\\');
                    string folder = dirs[dirs.Length - 1];
                    string folderAnt = dirs[dirs.Length - 2];
                    AddMenuItem("Set Genere as " + folder,nomApp,"/type dir /path " + fullPaths + " /setgen " + folder);
                    AddMenuItem("Set Album as " + folder,nomApp,"/type dir /path " + fullPaths + " /setalb " + folder);
                    AddMenuItem("Set Artist as " + folder,nomApp,"/type dir /path " + fullPaths + " /setart " + folder);
                    AddMenuItem("Set Genere as " + folderAnt + " " + folder,nomApp,"/type dir /path " + fullPaths + " /setgen " + folderAnt + " " + folder);
                    AddMenuItem("Set Album as " + folderAnt + " " + folder,nomApp,"/type dir /path " + fullPaths + " /setalb " + folderAnt + " " + folder);
                    AddMenuItem("Set Genere",nomApp,"/type dir /path " + fullPaths + " /owngen");
                    AddMenuItem("Set Album",nomApp,"/type dir /path " + fullPaths + " /ownalb");
                    AddMenuItem("Set Artist",nomApp,"/type dir /path " + fullPaths + " /ownart");
                    AddMenuItem("Edit All",nomApp,"/type dir /path " + fullPaths + " /all");
                }
                else
                {
                    bool guion = false;
                    bool containsNumber = false;
                    string[] separator;
                    int nume = 0;
                    if (directory.Split('\\').Last().Contains("_"))
                    {
                        guion = true;
                    }
                    directory = directory.Replace('_', ' ');
                    string archivo = directory.Split('\\').Last();
                    string nombre = "";
                    if (int.TryParse((separator = archivo.Split(' ')).First().Trim(), out nume))
                    {
                        containsNumber = true;
                        separator[0] = "";
                        archivo = string.Join(" ", separator);
                    }
                    if (int.TryParse((separator = archivo.Split('.')).First().Trim(), out nume))
                    {
                        containsNumber = true;
                        separator[0] = "";
                        archivo = string.Join(".", separator);
                        archivo = archivo.Substring(1);
                    }
                    if (archivo.Contains("."))
                    {
                        nombre = archivo.Substring(0, archivo.LastIndexOf('.'));
                    }
                    else
                    {
                        nombre = archivo;
                    }
                    if (archivo.Contains("-"))
                    {
                        List<String> datos = new List<String>(nombre.Split('-'));
                        List<String> rem = new List<String>();
                        int num = 0;
                        int subMenu = 0;
                        bool numFound = false;
                        foreach(String dato in datos)
                        {
                            if (dato.Trim().Equals(String.Empty))
                            {
                                rem.Add(dato);
                            }
                            else if (int.TryParse(dato.Trim(), out num))
                            {
                                numFound = true;
                                rem.Add(dato);
                            }
                        }
                        foreach (String quit in rem)
                        {
                            datos.Remove(quit);
                        }
                        switch (datos.Count)
                        {
                            case 0:
                                break;
                            case 1:
                                AddMenuItem("Set Song as " + datos[0].Trim(), nomApp, "/type file /path " + fullPaths + " /setson " + datos[0].Trim());
                                if(numFound)
                                    AddMenuItem("Remove number from filename");
                                else if(containsNumber)
                                    AddMenuItem("Remove initial number from filename");
                                break;
                            case 2:
                                AddMenuItem("Set Artist as " + datos[0].Trim(), nomApp, "/type file /path " + fullPaths + " /setart " + datos[0].Trim());
                                AddMenuItem("Set Album as " + datos[0].Trim(), nomApp, "/type file /path " + fullPaths + " /setalb " + datos[0].Trim());
                                AddMenuItem("Set Song as " + datos[1].Trim(), nomApp, "/type file /path " + fullPaths + " /setson " + datos[1].Trim());
                                AddMenuItem("Place on \\" + datos[0].Trim() + "\\", nomApp, "/type file /path " + fullPaths + " /dir \\" + datos[0].Trim() + "\\" + archivo);
                                if (numFound)
                                    AddMenuItem("Remove number from filename");
                                else if (containsNumber)
                                    AddMenuItem("Remove initial number from filename");
                                subMenu = AddMenu("Extras");
                                AddMenuItem("Set Artist as " + datos[0].Trim() + " and Song as " + datos[1].Trim(), nomApp, "/type file /path " + fullPaths + " /setart " + datos[0].Trim() + " /setson " + datos[1].Trim(), subMenu);
                                AddMenuItem("Set Artist as " + datos[1].Trim() + " and Song as " + datos[0].Trim(), nomApp, "/type file /path " + fullPaths + " /setart " + datos[1].Trim() + " /setson " + datos[0].Trim(), subMenu);
                                break;
                            case 3:
                                AddMenuItem("Set Artist as " + datos[0].Trim(), nomApp, "/type file /path " + fullPaths + " /setart " + datos[0].Trim());
                                AddMenuItem("Set Album as " + datos[0].Trim(), nomApp, "/type file /path " + fullPaths + " /setalb " + datos[0].Trim());
                                AddMenuItem("Set Artist as " + datos[1].Trim(), nomApp, "/type file /path " + fullPaths + " /setart " + datos[1].Trim());
                                AddMenuItem("Set Album as " + datos[1].Trim(), nomApp, "/type file /path " + fullPaths + " /setalb " + datos[1].Trim());
                                AddMenuItem("Set Song as " + datos[2].Trim(), nomApp, "/type file /path " + fullPaths + " /setson " + datos[2].Trim());
                                AddMenuItem("Place on \\" + datos[0].Trim() + "\\" + datos[1].Trim() + "\\", nomApp, "/type file /path " + fullPaths + " /dir \\" + datos[0].Trim() + "\\" + datos[1].Trim() + "\\" + archivo);
                                AddMenuItem("Place on \\" + datos[1].Trim() + "\\" + datos[0].Trim() + "\\", nomApp, "/type file /path " + fullPaths + " /dir \\" + datos[1].Trim() + "\\" + datos[0].Trim() + "\\" + archivo);
                                if (numFound)
                                    AddMenuItem("Remove number from filename");
                                else if (containsNumber)
                                    AddMenuItem("Remove initial number from filename");
                                subMenu = AddMenu("Extras");
                                AddMenuItem("Set Artist as " + datos[0].Trim() + " and Album as " + datos[1].Trim() + " and Song as " + datos[2].Trim(), nomApp, "/type file /path " + fullPaths + " /setart " + datos[0].Trim() + " /setalb " + datos[1].Trim() + " /setson " + datos[2].Trim(), subMenu);
                                AddMenuItem("Set Artist as " + datos[1].Trim() + " and Album as " + datos[0].Trim() + " and Song as " + datos[2].Trim(), nomApp, "/type file /path " + fullPaths + " /setart " + datos[1].Trim() + " /setalb " + datos[0].Trim() + " /setson " + datos[2].Trim(), subMenu);
                                break;
                        }
                        if (guion)
                        {
                            AddMenuItem("Remove underscores '_'");
                        }
                        AddMenuItem("Set Genere", nomApp, "/type file /path " + fullPaths + " /owngen");
                        AddMenuItem("Set Album", nomApp, "/type file /path " + fullPaths + " /ownalb");
                        AddMenuItem("Set Artist", nomApp, "/type file /path " + fullPaths + " /ownart");
                        AddMenuItem("Set Song", nomApp, "/type file /path " + fullPaths + " /ownson");
                        AddMenuItem("Edit All", nomApp, "/type file /path " + fullPaths + " /all");


                    }
                    else
                    {
                        AddMenuItem("Set Song as " + nombre.Trim(),nomApp,"/type file /path " + fullPaths + " /setson " + nombre.Trim());
                        AddMenuItem("Set Genere",nomApp,"/type file /path " + fullPaths + " /owngen");
                        AddMenuItem("Set Album",nomApp,"/type file /path " + fullPaths + " /ownalb");
                        AddMenuItem("Set Artist",nomApp,"/type file /path " + fullPaths + " /ownart");
                        if (guion)
                        {
                            AddMenuItem("Remove underscores '_'");
                        }
                        if (containsNumber)
                            AddMenuItem("Remove initial number from filename");
                        AddMenuItem("Edit All",nomApp,"/type file /path " + fullPaths + " /all");
                    }
                }
            }
            else
            {
                if (typeDirOrFile==1)
                {
                    AddMenuItem("Set Genere as (FolderName)", nomApp, "/type dirs /path " + fullPaths + " /setgen");
                    AddMenuItem("Set Album as (FolderName)", nomApp, "/type dirs /path " + fullPaths + " /setalb");
                    AddMenuItem("Set Artist as (FolderName)", nomApp, "/type dirs /path " + fullPaths + " /setart");
                    AddMenuItem("Set Genere as (LastFolder) (CurrentName)", nomApp, "/type dirs /path " + fullPaths + " /setgenboth");
                    AddMenuItem("Set Album as (LastFolder) (CurrentName)", nomApp, "/type dirs /path " + fullPaths + " /setalbboth");
                    AddMenuItem("Set Genere", nomApp, "/type dirs /path " + fullPaths + " /owngen");
                    AddMenuItem("Set Album", nomApp, "/type dirs /path " + fullPaths + " /ownalb");
                    AddMenuItem("Set Artist", nomApp, "/type dirs /path " + fullPaths + " /ownart");
                    AddMenuItem("Edit All", nomApp, "/type dirs /path " + fullPaths + " /all");
                }
                else if (typeDirOrFile==0)
                {
                    int startSize = paths[0].Split('-').Length;
                    int cont = 0;
                    foreach (String archivo in paths)
                    {
                        if (startSize == archivo.Split('-').Length)
                        {
                            cont++;
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (cont == paths.Count)
                    {
                        int subMenu = 0;
                        switch (startSize)
                        {
                            case 0:
                                break;
                            case 1:
                                AddMenuItem("Set Song as (Name)", nomApp, "/type files /path " + fullPaths + " /setson");
                                break;
                            case 2:
                                AddMenuItem("Set Artist as (Name) - ***", nomApp, "/type files /path " + fullPaths + " /setart 0");
                                AddMenuItem("Set Album as  (Name) - ***", nomApp, "/type files /path " + fullPaths + " /setalb 0");
                                AddMenuItem("Set Song as     ***  - (Name)", nomApp, "/type files /path " + fullPaths + " /setson 1");
                                AddMenuItem("Place on     \\(Name) - ***\\", nomApp, "/type files /path " + fullPaths + " /dir 0");
                                subMenu = AddMenu("Extras");
                                AddMenuItem("Set Artist as (Name) - ***    and Song as   ***  - (Name)", nomApp, "/type files /path " + fullPaths + " /setart 0 /setson 1", subMenu);
                                AddMenuItem("Set Artist as   ***  - (Name) and Song as (Name) -  ***"  , nomApp, "/type files /path " + fullPaths + " /setart 1 /setson 0", subMenu);
                                break;
                            case 3:
                                AddMenuItem("Set Artist as (Name) -  ***   -  ***", nomApp, "/type files /path " + fullPaths + " /setart 0");
                                AddMenuItem("Set Album as  (Name) -  ***   -  ***", nomApp, "/type files /path " + fullPaths + " /setalb 0");
                                AddMenuItem("Set Artist as  ***   - (Name) -  ***", nomApp, "/type files /path " + fullPaths + " /setart 1");
                                AddMenuItem("Set Album as   ***   - (Name) -  ***", nomApp, "/type files /path " + fullPaths + " /setalb 1");
                                AddMenuItem("Set Song as    ***   -  ***   - (Name)", nomApp, "/type files /path " + fullPaths + " /setson 2");
                                AddMenuItem("Place on     \\(Name) -  ***   - ***\\  ***  - (Name) - ***\\", nomApp, "/type files /path " + fullPaths + " /dir 0,1");
                                AddMenuItem("Place on     \\ ***   - (Name) - ***\\(Name) -  ***   - ***\\", nomApp, "/type files /path " + fullPaths + " /dir 1,0");
                                subMenu = AddMenu("Extras");
                                AddMenuItem("Set Artist as (Name) -  ***   -  *** and Album as  ***   - (Name) -  *** and Song as *** - *** - (Name)", nomApp, "/type file /path " + fullPaths + " /setart 0 /setalb 1 /setson 2", subMenu);
                                AddMenuItem("Set Artist as  ***   - (Name) -  *** and Album as (Name) -  ***   -  *** and Song as *** - *** - (Name)", nomApp, "/type file /path " + fullPaths + " /setart 1 /setalb 0 /setson 2", subMenu);
                                break;
                            default:
                                break;
                        }
                    }
                    else
                    {
                        AddMenuItem("Set Smart Renaming", nomApp, "/type files /path " + fullPaths + " /setsmart");
                    }
                    AddMenuItem("Remove numbers from filenames");
                    AddMenuItem("Remove _ from files");
                    AddMenuItem("Set Genere", nomApp, "/type files /path " + fullPaths + " /owngen");
                    AddMenuItem("Set Album", nomApp, "/type files /path " + fullPaths + " /ownalb");
                    AddMenuItem("Set Artist", nomApp, "/type files /path " + fullPaths + " /ownart");
                    AddMenuItem("Edit All", nomApp, "/type files /path " + fullPaths + " /all");
                    if (!extensions.Contains(".M3U"))
                        AddMenuItem("Generate PlayList");
                }
                else
                {
                    AddMenuItem("Set Genere", nomApp, "/type dirfile /path " + fullPaths + " /owngen");
                    AddMenuItem("Set Album", nomApp, "/type dirfile /path " + fullPaths + " /ownalb");
                    AddMenuItem("Set Artist", nomApp, "/type dirfile /path " + fullPaths + " /ownart");
                    AddMenuItem("Edit All", nomApp, "/type dirfile /path " + fullPaths + " /all");
                }
            }


            //throw new NotImplementedException();
            return ReturnStatus();
        }

        #endregion
    }
}
